package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewCartServlet extends HttpServlet {
    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    CatalogService catalogService;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Cart cart = (Cart)session.getAttribute("cart");
        catalogService = new CatalogService();
        List<CartList> cartLists = new ArrayList<>();
        String username = (String) session.getAttribute("username");
        cartLists = catalogService.getCartListByUsername(username);
        if (cart == null) {
            cart = new Cart();
            for (int i=0;i<cartLists.size();i++){
                CartItem cartItem = new CartItem();
                Item item = catalogService.getItem(cartLists.get(i).getItemid());
                cartItem.setItem(item);
                cartItem.setInStock(Boolean.parseBoolean(cartLists.get(i).getInStock()));
                cartItem.setQuantity(cartLists.get(i).getQuantity());
//                System.out.println(cartLists.get(i).getQuantity());
                cart.addItem(item,Boolean.parseBoolean(cartLists.get(i).getInStock()),cartItem.getQuantity());
            }
            session.setAttribute("cart",cart);
        }else {
            for (int i=0;i<cartLists.size();i++){
                CartItem cartItem = new CartItem();
                Item item = catalogService.getItem(cartLists.get(i).getItemid());
                cartItem.setItem(item);
                cartItem.setInStock(Boolean.parseBoolean(cartLists.get(i).getInStock()));
                cartItem.setQuantity(cartLists.get(i).getQuantity());
                cart.addItem(item,Boolean.parseBoolean(cartLists.get(i).getInStock()),cartItem.getQuantity());
            }
            session.setAttribute("cart",cart);
        }
        Account account = (Account)session.getAttribute("account");

        if(account != null){
            HttpServletRequest httpRequest= request;
            String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + " Check the cart " + cart;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        request.getRequestDispatcher(VIEW_CART).forward(request,response);
    }
}
